package cn.afactory.tygz.ui.base.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.model.SnsSubjectDetail;
import cn.afactory.tygz.ui.base.adapter.CommentListAdapter;
import cn.afactory.tygz.ui.base.adapter.NearByForumsAdapter;
import cn.afactory.tygz.ui.base.adapter.SubjectAgreeUserGirdAdapter;
import cn.afactory.tygz.ui.base.adapter.SubjectImgGirdAdapter;
import cn.afactory.tygz.ui.base.utils.DateUtil;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.ScreenUtil;
import cn.afactory.tygz.ui.base.utils.StringUtils;
import cn.afactory.tygz.ui.base.utils.sns.ViewTools;

/**
 * Created by Kenneth on 15/9/24.
 */
public class SubjectDetailHeaderView extends LinearLayout {

    private Context mContext;

    private SnsSubjectDetail mSnsSubjectDetail;

    private SubjectImgGirdAdapter mImgGirdAdapter;
    private SubjectAgreeUserGirdAdapter mAgreeUserGirdAdapter;
    private int mImageWidth, mImageHeight;

    private ImageView nearByForumsIcon;
    private TextView nearByForumsName;
    private TextView nearByForumsAge;
    private View nearByForumsAgeRight;
    private TextView nearByForumsCity;
    private TextView nearByForumsTime;
    private TextView nearByForumsCompany;
    private TextView nearByForumsDistance;

    private TextView nearByForumsText;
    private AutoGridView nearByForumsPics;
    private TextView nearByForumsTopic;
    private RelativeLayout nearByFormsFromTopicLayout;

    private TextView nearByForumsLikeCount;
    private AutoGridView nearByForumsLikePeople;
    private TextView nearByForumsShare;

    private TextView nearByForumsCommentCount;
    private View nearByForumsCommentDivision;

    public SubjectDetailHeaderView(Context context, AttributeSet attrs, SnsSubjectDetail subjectDetail) {
        super(context, attrs);
        mSnsSubjectDetail = subjectDetail;
        mContext = context;
        mImageWidth = (ScreenUtil.getScreenWidth(context) - context.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding10) * 2
                - context.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2) * 2) / 3;
        mImageHeight = mImageWidth + context.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
        initView(context, attrs);
    }

    public void setmSnsSubjectDetail(SnsSubjectDetail mSnsSubjectDetail) {
        this.mSnsSubjectDetail = mSnsSubjectDetail;
        bindData();
    }

    private void initView(Context context, AttributeSet attrs){

        View rootView = LayoutInflater.from(context).inflate(R.layout.fg_subject_detail_header, this, true);

        nearByForumsIcon = (ImageView)rootView.findViewById(R.id.nearby_forums_icon);
        nearByForumsName = (TextView)rootView.findViewById(R.id.nearby_forums_name);
        nearByForumsAge = (TextView)rootView.findViewById(R.id.nearby_forums_age);
        nearByForumsAgeRight = (View)rootView.findViewById(R.id.nearby_forums_age_right_view);
        nearByForumsCity = (TextView)rootView.findViewById(R.id.nearby_forums_city);
        nearByForumsTime = (TextView)rootView.findViewById(R.id.nearby_forums_time);
        nearByForumsCompany = (TextView)rootView.findViewById(R.id.nearby_forums_company);
        nearByForumsDistance = (TextView)rootView.findViewById(R.id.nearby_forums_distance);

        nearByForumsText = (TextView)rootView.findViewById(R.id.nearby_forums_text);
        nearByForumsPics = (AutoGridView)rootView.findViewById(R.id.nearby_forums_pic);
        nearByForumsTopic = (TextView)rootView.findViewById(R.id.nearby_forums_topic);
        nearByFormsFromTopicLayout = (RelativeLayout)rootView.findViewById(R.id.nearby_from_topic_layout);

        nearByForumsLikeCount = (TextView)rootView.findViewById(R.id.nearby_forums_like_count);
        nearByForumsLikePeople = (AutoGridView)rootView.findViewById(R.id.nearby_forums_like_list);
        nearByForumsShare = (TextView)rootView.findViewById(R.id.nearby_forums_share_button);

        nearByForumsCommentDivision = (View)rootView.findViewById(R.id.nearby_forums_comment_division);
        nearByForumsCommentCount = (TextView)rootView.findViewById(R.id.nearby_forums_comment_count);

        bindData();
    }

    private void bindData() {
        if (null == mSnsSubjectDetail) {
            return;
        }
        //bind data
        if (mSnsSubjectDetail.userIcon != null) {
            BaseImgView.loadimg(nearByForumsIcon,
                    mSnsSubjectDetail.userIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            nearByForumsIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }

        nearByForumsIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onClickIcon(mSnsSubjectDetail);
                }
            }
        });

        if (mSnsSubjectDetail.nickName != null) {
            nearByForumsName.setText(mSnsSubjectDetail.nickName);
        }

        if (!TextUtils.isEmpty(mSnsSubjectDetail.city) && mSnsSubjectDetail.birth != 0) {
            nearByForumsAgeRight.setVisibility(VISIBLE);
        } else {
            nearByForumsAgeRight.setVisibility(GONE);
        }

        if (mSnsSubjectDetail.birth != 0) {
            nearByForumsAge.setVisibility(VISIBLE);
            nearByForumsAge.setText(DateUtil.getAgeByNorth(mSnsSubjectDetail.birth) + "岁");
        } else {
            nearByForumsAge.setVisibility(GONE);
        }

        if (null != mSnsSubjectDetail.city && mSnsSubjectDetail.city.trim().length()>0) {
            nearByForumsCity.setVisibility(VISIBLE);
            nearByForumsCity.setText(mSnsSubjectDetail.city);
        } else {
            nearByForumsCity.setVisibility(GONE);
        }

        if (null != mSnsSubjectDetail.company && mSnsSubjectDetail.company.trim().length()>0) {
            nearByForumsCompany.setVisibility(VISIBLE);
            nearByForumsCompany.setText(mSnsSubjectDetail.company);
        } else {
            nearByForumsCompany.setVisibility(GONE);
        }

        nearByForumsDistance.setText("距离: " + StringUtils.formartDistance(mSnsSubjectDetail.distance));

        if (null != mSnsSubjectDetail.content && mSnsSubjectDetail.content.trim().length() > 0) {
            nearByForumsText.setText(mSnsSubjectDetail.content.trim());
            nearByForumsText.setVisibility(View.VISIBLE);
        } else {
            nearByForumsText.setVisibility(View.GONE);
        }

        if (null != mSnsSubjectDetail.contentImg && mSnsSubjectDetail.contentImg.size() > 0) {
            nearByForumsPics.setVisibility(View.VISIBLE);
            mImgGirdAdapter = new SubjectImgGirdAdapter(mContext, mSnsSubjectDetail.contentImg, mImageWidth, mImageHeight);
            nearByForumsPics.setAdapter(mImgGirdAdapter);
            nearByForumsPics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickImage(position, mSnsSubjectDetail.contentImg);
                    }
                }
            });
        }else{
            nearByForumsPics.setVisibility(View.GONE);
        }

        if (null != mSnsSubjectDetail.topicName && mSnsSubjectDetail.topicName.trim().length() > 0) {
            nearByForumsTopic.setText("#"+mSnsSubjectDetail.topicName+"#");
            nearByFormsFromTopicLayout.setVisibility(View.VISIBLE);
            nearByForumsTopic.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickTopic(mSnsSubjectDetail.topicName.trim(), mSnsSubjectDetail.topicId);
                    }
                }
            });
        } else {
            nearByFormsFromTopicLayout.setVisibility(View.GONE);
        }

        ViewTools.setPraiseView(nearByForumsLikeCount, mSnsSubjectDetail.isAgree == 1, mSnsSubjectDetail.agree);
        nearByForumsLikeCount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onPraise((TextView) v, mSnsSubjectDetail);
                }
            }
        });

        nearByForumsShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onClickShare(mSnsSubjectDetail);
                }
            }
        });

        if (null != mSnsSubjectDetail.agreeUser && mSnsSubjectDetail.agreeUser.size() > 0) {
            int mIconWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.agree_people_icon_width);
            int mIconHeight = mIconWidth + mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
            nearByForumsLikePeople.setVisibility(VISIBLE);
            mAgreeUserGirdAdapter = new SubjectAgreeUserGirdAdapter(mContext, mSnsSubjectDetail.agreeUser, mIconWidth, mIconHeight);
            nearByForumsLikePeople.setAdapter(mAgreeUserGirdAdapter);

            nearByForumsLikePeople.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    NaviUtils.gotoUserList(mContext,
                            -1,
                            null,
                            mSnsSubjectDetail.id,
                            mSnsSubjectDetail.agree,
                            UserListType.PRASIZE_LIST);
                }
            });
        } else {
            nearByForumsLikePeople.setVisibility(GONE);
        }

        if (mSnsSubjectDetail.comments > 0) {
            nearByForumsCommentCount.setVisibility(VISIBLE);
            nearByForumsCommentDivision.setVisibility(VISIBLE);
            nearByForumsCommentCount.setText("评论(" + mSnsSubjectDetail.comments + ")");
        } else {
            nearByForumsCommentCount.setVisibility(GONE);
            nearByForumsCommentDivision.setVisibility(GONE);
        }
    }

    private NearByForumsAdapter.OnItemClickListener mItemClickListener;
    public void setOnItemClickLsn(NearByForumsAdapter.OnItemClickListener lsn){
        mItemClickListener = lsn;
    }
}
